(function ($) {
	var sourceData;
	var perInfo,nurseid
	var keysArr = [{'key':'TranStDate','type':'date','text':'开始日期'},{'key':'TranEndDate','type':'date','text':'结束日期'},
		{'key':'TranLevel','type':'list','text':'级别'},{'key':'TranProject','type':'list','text':'培训项目'},
		{'key':'TranType','type':'list','text':'培训类型'},{'key':'TranProfession','type':'list','text':''},
		{'key':'TranMode','type':'list','text':'培训方式'},{'key':'TranContext','type':'input','text':'培训内容'},
		{'key':'TranRemark','type':'input','text':''},{'key':'TranCharge','type':'list','text':'负责人'},
		{'key':'TranNurse','type':'list','text':'培训对象'}]
	$.init();
	$.plusReady(function() {
		baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	function submitNewData(){
		var status = this.getAttribute('data-id');
		//TranStDate|2023-07-26^TranEndDate|2023-07-26^TranLevel|W^TranProject|50||1^TranType|49||1^TranProfession|52||2^TranMode|51||1^TranContext|培训内容^TranRemark|备注^TranCharge|338^TranNurse|1^TranStatus|N^rw|2^TranStatus|N
		var parr = ''
		for (var i = 0; i < keysArr.length;i++) {
			if (i > 0) {
				parr +='^'
			}
			var oneKey = keysArr[i]
			var type = oneKey['type']
			var id = oneKey['key']
			var tip = '请选择'
			var str = ''
			var dom = $('#'+id)[0];
			if (type == "input") {
				str = dom.value
				tip = '请输入'
			}else if (type == 'date') {
				str = dom.innerText
				if (str.indexOf('日期') != -1) {
					str = ''
				}
			}else{
				str = dom.getAttribute('data-id')
			}
			if (str == '' && oneKey['text']!='') {
				mui.toast(tip+oneKey['text'])
				showEmptyAnimation(id)
				return;
			}
			parr += id+'|'+str
		}
		parr += '^TranStatus|N^rw|'
		if (sourceData) {
			parr += sourceData['rw'];
		}
		var pdict = {'loginId':nurseid,'parr':parr}
		var paramDict = {'method':'web.INMTeaComm:SaveNurseTrain','params':JSON.stringify(pdict),'type':'Method'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			var msg = result['msg']
			if (msg == '1') {
				plus.webview.getWebviewById('../WKPlatformPages/teach/teachList.html').evalJS('detailChanged()')
				mui.back()
				mui.toast('保存成功')
			}else{
				mui.toast(msg)
			}
		}, function(errorStr) {
			mui.toast('请重试')
		});
	}
	
	function publishTrain(){
		var pdict = {'loginId':nurseid,'parr':sourceData['rw']}
		var paramDict = {'method':'web.INMTeaComm:PublishNurseTrain','params':JSON.stringify(pdict),'type':'Method'}
		$.DHCWebService(paramDict, function(result) {
			var msg = result['msg']
			if (msg == '1') {
				plus.webview.getWebviewById('../WKPlatformPages/teach/teachList.html').evalJS('detailChanged()')
				mui.back()
			}else{
				mui.toast(msg)
			}
		}, function(errorStr) {
			mui.toast('请重试')
		});
	}
	
	function updateUI(){
		for (var i = 0; i < keysArr.length;i++) {
			var oneKey = keysArr[i]
			var type = oneKey['type']
			var keyStr = oneKey['key']
			var dom = $('#'+keyStr)[0];
			var value = sourceData[keyStr]
			if (type == "input") {
				dom.value = value
			}else if (type == 'date') {
				dom.innerText = value
				if (value != '') {
					dom.classList.remove('noTextTip')
				}
			}else{
				dom.setAttribute('data-id',value)
				if (keyStr == 'TranLevel') {
					var levelDict = {'H':'院级','L':'科室','W':'病区'}
					var desc = levelDict[value]
					dom.innerText = desc
				}else{
					dom.innerText = sourceData[keyStr+'Desc']
				}
				
			}
		}
	}
	
	//选择日期
	function selectDate(){
		var _self = this;
		plus.nativeUI.pickDate(function(e) {
			var d = e.date;
			var dateStr = d.getFullYear()+'-'
			if (d.getMonth() < 9) {
				dateStr += '0'
			}
			dateStr += (d.getMonth() + 1)+'-'
			if (d.getDate() < 10) {
				dateStr += '0'
			}
			dateStr += d.getDate()
			_self.classList.remove('noTextTip')
			_self.innerText = dateStr;
			resetZkRefresh();
		}, function(e) {}, {});
	}
	
	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		var self = plus.webview.currentWebview()
		sourceData = self.data
		if (sourceData) { //修改
			updateUI()
			$('#submitBtn')[0].removeAttribute('style')
		}
		perInfo = self.perInfo
		nurseid = perInfo['loginID']
		$('#CreatorName')[0].innerText = perInfo['loginName']
		//$('#applyLoc')[0].innerText = perInfo['loginWardDesc']
		//关闭按钮
		mui('body').on('tap','#closeBtn',mui.back)
		//保存提交按钮
		mui('body').on('tap','#saveBtn',submitNewData)
		mui('body').on('tap','#submitBtn',publishTrain)
		//下拉列表
		mui('#UL').on('tap','.listType',downListElementTapped);
		mui('#UL').on('tap','.authText',function(){
			blurAllInput()
			var dom = this
			setTimeout(function() {
				toChosePerson(dom.id,'单选') //防止切换页面时键盘未隐藏
			}, 100);
		}); //选人
		
		// 日期
		mui('#UL').on('tap','.dateType',selectDate);
		window.addEventListener('resize',windowResize)
	}
	
})(mui)
function selectPerson(domId,params){
	var dom = mui('#'+domId)[0]
	var html='',idStr=''
	for(var i = 0; i < params.length;i++){
		var onePer = params[i]
		idStr = onePer['per']
		html = onePer['perName']
	}
	dom.innerHTML = html;
	dom.setAttribute('data-id',idStr)
}
